﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Configuration;

namespace YY.Common
{    
    public class OtherCommon
    {
        /// <summary>
        /// 类型对象成员克隆
        /// </summary>
        /// <param name="os">克隆源</param>
        /// <param name="od">目标对象</param>        
        public static bool objEval(object os, object od)
        {
            try
            {
                PropertyInfo[] properties = os.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                foreach (PropertyInfo item in properties)
                {
                    string name = item.Name;
                    object value = item.GetValue(os, null);
                    Type t = os.GetType();
                    t.GetProperty(name).SetValue(od, value, null);
                }
                return true;
            }
            catch { }
            return false;

        }

        /// <summary>
        /// 类型对象成员克隆
        /// </summary>
        /// <param name="os">克隆源</param>
        /// <param name="od">目标对象</param>
        /// <param name="exceptNames">排除的属性成员名称</param>
        /// <returns></returns> 
        public static bool objEval(object os, object od, List<string> exceptNames)
        {
            try
            {
                PropertyInfo[] properties = os.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                foreach (PropertyInfo item in properties)
                {
                    string name = item.Name;
                    if (!exceptNames.Contains(name))
                    {
                        object value = item.GetValue(os, null);
                        Type t = os.GetType();
                        t.GetProperty(name).SetValue(od, value, null);
                    }
                }
                return true;
            }
            catch { }
            return false;

        }
    }
}
